var path = require('path'), 
    express = require('express'),
    consolidate = require('consolidate');
    app = express();

// set static dir
// access demo: http://127.0.0.1:8080/static/images/xxx.png
app.use('/static', express.static(path.resolve(__dirname, 'public'))); 

// set views dir
app.set('views', path.resolve(__dirname, 'views'));
// set .html as the default extension
app.set('view engine', 'html');
// assign the swig engine to .html files
app.engine('html', consolidate.swig);

app.use('/', require('./routers/main.js'));

app.use(function (req, res, next) {
    res.status(404).send('Sorry cant find that!');
});

app.use(function (err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

var server = app.listen(8080, 'localhost', function () {
    var address = server.address();
    var host = address.address;
    var port = address.port;

    console.log('App listening at http://%s:%s', host, port);
});